package com.xxx.video.login.activitys;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.xxx.video.R;
import com.xxx.video.commen.activitys.BaseActivity;
import com.xxx.video.commen.events.LoginSuccessEvent;
import com.xxx.video.commen.utils.FontCacheUtils;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.commen.views.FontTextView;
import com.xxx.video.login.beans.RegisterBundleInfoBean;
import com.xxx.video.login.presenters.IRegisterFirstView;
import com.xxx.video.login.presenters.RegisterFirstPresenter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class RegisterFirstActivity extends BaseActivity<RegisterFirstPresenter> implements IRegisterFirstView {
    private static final int REQUEST_PHONE_PERMISSION = 201;


    @BindView(R.id.et_register_first_activity_user_name)
    EditText etRegisterFirstActivityUserName;
    @BindView(R.id.et_register_first_activity_pwd)
    EditText etRegisterFirstActivityPwd;
    @BindView(R.id.et_register_first_activity_pwd_again)
    EditText etRegisterFirstActivityPwdAgain;
    @BindView(R.id.et_register_first_activity_email)
    EditText etRegisterFirstActivityEmail;
    @BindView(R.id.et_register_first_activity_phone)
    EditText etRegisterFirstActivityPhone;
    @BindView(R.id.et_register_first_activity_nick_name)
    EditText etRegisterFirstActivityNickName;
    private String[] permissions = {Manifest.permission.READ_SMS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_PHONE_NUMBERS};

    @Override
    public boolean isOpenEventBus() {
        return true;
    }

    @Override
    protected RegisterFirstPresenter createPresenter() {
        return new RegisterFirstPresenter(this, this);
    }

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        showActionBar();
        StatusBarUtil.getInstance().setStatusBarTransparent(this, true);
        setActionBarTitle(R.string.title_register_first_activity);
        Typeface customFont = FontCacheUtils.getInstance().getTypeface(FontTextView.ttf3, this);
        etRegisterFirstActivityUserName.setTypeface(customFont);
        etRegisterFirstActivityPwd.setTypeface(customFont);
        etRegisterFirstActivityPwdAgain.setTypeface(customFont);
        etRegisterFirstActivityEmail.setTypeface(customFont);
        etRegisterFirstActivityPhone.setTypeface(customFont);
        etRegisterFirstActivityNickName.setTypeface(customFont);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_register_first;
    }

    @Override
    public void initData(Bundle extras) {
        showLayoutStatus(1);
        ActivityCompat.requestPermissions(this, permissions, REQUEST_PHONE_PERMISSION);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_PHONE_PERMISSION:
                try {
                    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                    String phone = telephonyManager.getLine1Number();
                    etRegisterFirstActivityPhone.setText(phone);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void showRootLayoutStatus(int status) {

    }

    @OnClick({R.id.tv_register_first_activity_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_register_first_activity_next:
                getPresenter().register();
                break;
            default:
                break;
        }
    }

    @Override
    public void startRegisterSecondActivity(String userName, String pwd, String email, String phone, String nickName) {
        Bundle bundle = new Bundle();
        RegisterBundleInfoBean registerBundleInfoBean = new RegisterBundleInfoBean.Builder()
                .userName(userName)
                .pwd(pwd)
                .email(email)
                .phone(phone)
                .nickName(nickName)
                .build();
        bundle.putParcelable("RegisterBundleInfo", registerBundleInfoBean);
        intent2Activity(RegisterSecondActivity.class, bundle);
    }

    @Override
    public String getUserName() {
        return etRegisterFirstActivityUserName.getText().toString().trim();
    }

    @Override
    public String getPwd() {
        return etRegisterFirstActivityPwd.getText().toString().trim();
    }

    @Override
    public String getPwdAgain() {
        return etRegisterFirstActivityPwdAgain.getText().toString().trim();
    }

    @Override
    public String getPhone() {
        return etRegisterFirstActivityPhone.getText().toString().trim();
    }

    @Override
    public String getEmail() {
        return etRegisterFirstActivityEmail.getText().toString().trim();
    }

    @Override
    public String getNickName() {
        return etRegisterFirstActivityNickName.getText().toString().trim();
    }

    /**
     * login Success
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginSuccess(LoginSuccessEvent loginSuccessEvent) {
        finish();
    }
}
